<?php


namespace app\admin\controller;

use app\common\model\News;
use think\File;
use think\Image;
use think\Controller;
use think\Request;

class NewsController extends BaseController{

    public function index(){
        $adminData = session('adminData');

        $newsModel = new News();
        $newsList = $newsModel->getAllNewsList();

        return view('index', ['newsList' => $newsList,'adminData'=>$adminData]);
    }


    public function upload(){

        

        $token = Request::instance()->header('token');
        //$token权限校验。。。
        $files = Request::instance()->file('image');
        foreach ($files as $k => $file) {  
            $conf=array(  
                  'size'=>50000000,  
                  'ext'=>'jpg,png,gif,jpeg',  
              );  
            $info = $files->validate($conf)->move(ROOT_PATH . 'public' . DS . 'uploads');  
            if($info){  
              $saveName = $info->getSaveName();   
              $save_url =  $info->getFilename(); //保存数据库的路径  
              $this->img='aaaaaaaaaaaaaaaaaa';
              return $saveName;  
        
            }else{  
              echo $files->getError();  
              return json(['msg' => '上传失败']);  
            }  
            // md5(uniqid(microtime(true),true));  
          }  
    }



    public function add(){
        
        if (Request::instance()->isPost()) {
            $title = trim(input('post.title'));
            $detail = trim(input('post.detail'));
            $imgurl = trim(input('post.imgurl'));
            $recommend = trim(input('post.recommend'));
            $remark = trim(input('post.remark'));

            if($title == '' || $detail == '' || $remark == '' ){
                return json(array('code'=>1,'msg'=>'标题、简介和内容不能为空'));
            }

            if($imgurl == ''){
                return json(array('code'=>1,'msg'=>'请上传封面图'));
            }

            $adminData = session('adminData');
            $newsModel = new News();
            $newsModel->add($title, $remark, $detail, $adminData['adminId'], $imgurl, $recommend);
            return json(array('code'=>0,'msg'=>'添加成功'));
        }else{
            $adminData = session('adminData');

            return view('add', ['adminData'=>$adminData]);
        }
    }


    public function edit(){
        if (Request::instance()->isPost()) {
            $nid =  input('post.nid');
            $title = trim(input('post.title'));
            $detail = trim(input('post.detail'));
            $imgurl = trim(input('post.imgurl'));
            $recommend = trim(input('post.recommend'));
            $remark = trim(input('post.remark'));

            if($title == '' || $detail == '' || $remark == '' ){
                return json(array('code'=>1,'msg'=>'标题、简介和内容不能为空'));
            }

            if($imgurl == ''){
                $newsModel = new News();
                $newsData = $newsModel->getNewsById($nid);
                $imgurl = $newsData[0]['imgurl'];
            }

            $newsModel = new News();
            $newsData = $newsModel->getNewsById($nid);

            if(count($newsData)<1){
                return json(array('code'=>1,'msg'=>'没有该新闻'));
            }else{
                $newsModel->edit($nid, $title, $remark, $detail, $imgurl, $recommend);
                return json(array('code'=>0,'msg'=>'修改成功'));
            }
        }else{
            $nid = input('get.nid');
            if(!$nid){
                $this->error('链接错误,返回首页','/admin/news/index');
            }

            $newsModel = new News();
            $newsData = $newsModel->getNewsById($nid)[0];

            $adminData = session('adminData');

            return view('edit', ['newsData'=>$newsData,'adminData'=>$adminData]);
        }
    }

    public function delete(){
        if (Request::instance()->isPost()) {
            $nid = $username = input('post.nid');

            $newsModel = new News();
            $newsData = $newsModel->getNewsById($nid);

            if(count($newsData)<1){
                return json(array('code'=>1,'msg'=>'没有该新闻'));
            }else{
                $newsModel->deleteNews($nid);
                return json(array('code'=>0,'msg'=>'删除成功'));
            }
        }
    }

    public function status(){
        if (Request::instance()->isPost()) {
            $nid = $username = input('post.nid');
            $status = $username = input('post.status');

            $newsModel = new News();
            $newsData = $newsModel->getNewsById($nid);

            if(count($newsData)<1){
                return json(array('code'=>1,'msg'=>'没有该新闻'));
            }else {
                $newsModel->status($nid, $status);
                return json(array('code' => 0, 'msg' => '修改成功'));
            }
        }
    }

}
